﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_BLL;
using V5_Model;
using V5_Common;

namespace V5_EShop.Admin
{
    public partial class Admin_HotKeyWord : BasicPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Action = Input.SetString(Request["action"]);
                switch (Action.ToLower().Trim())
                {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        Bind_gvList();
                        break;
                }
            }
        }

        private void Bind_gvList()
        {
            this.MultiViewList.ActiveViewIndex = 0;
            int Count = 0;
            this.gvList.DataSource = new BLLHotKeyWord().GetList(Pager.PageSize, Pager.CurrentPageIndex, "", ref Count);
            this.Pager.RecordCount = Count;
            this.gvList.DataBind();
        }

        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            Bind_gvList();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            BLLHotKeyWord bll = new BLLHotKeyWord();
            ModelHotKeyWord model = new ModelHotKeyWord();
            int id = Input.SetInt(this.hfID.Value);
            string HotKeyName = this.txtKeyWordTitle.Text;
            string HotKeyUrl = this.txtKeyWordUrl.Text;
            int IsShow = Input.SetInt(this.DropDownList1.SelectedValue);
            model.HotKeyName = HotKeyName;
            model.HotKeyUrl = HotKeyUrl;
            model.IsShow = IsShow;
            model.OrderID = 0;
            if (id == 0)
            {
                bll.Add(model);
            }
            else if (id > 0)
            {
                model.ID = id;
                bll.Update(model);
            }
            this.Bind_gvList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string eName = Input.SetString(e.CommandName);
            int id = Input.SetInt(e.CommandArgument);
            switch (eName.ToLower().Trim())
            {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
            }
        }

        private void Bind_DataEdit(int id)
        {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelHotKeyWord model = new BLLHotKeyWord().GetModel(id);
            if (model != null)
            {
                this.txtKeyWordTitle.Text = model.HotKeyName;
                this.txtKeyWordUrl.Text = model.HotKeyUrl;
                this.DropDownList1.SelectedValue = model.IsShow.ToString();

                this.hfID.Value = model.ID.ToString();
                this.btnSave.Text = "修改保存";
                this.lblTitle.Text = "修改热门关键字";
            }
        }

        private void Bind_Delete(int id)
        {
            new BLLHotKeyWord().Delete(id);
            this.Bind_gvList();
        }
    }
}
